<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Serve;

use DB;

class HotController extends Controller
{
       // 检测用户是否登录，无登录跳转到登录页面
    public function __construct()
    {
       $this->middleware('auth');
    }
    //热门服务首页展示
    public function index(){

    	//取出所有服务
    	$serve = Serve::get();

        //取出热门服务
        $hot = DB::table('serve_hot')
                ->LeftJoin('serves','serve_hot.sid','=','serves.id')
                ->select('serves.*')
                ->OrderBy('serves.id','desc')
                ->get();

    	return view('staticpages.remen.index',compact('hot','serve'));
    }


    //获取热门表单数据
    public function store(Request $request){

    	if(!empty($request->id)){

    		$status = array();

    		foreach($request->id as $rv){

    			$status[] = DB::table('serve_hot')->insert(['sid'=>$rv,'status'=>'1']);

    		}

    		if(!in_array(false, $status)){

    			return back();

    		}else{

    			return '录入失败';
    		}

    	}else{

    		return '请选择后再提交';
    	}
    	
    }

    //删除热门服务
    public function edit($id){

        //删除想要删除的热门
        $status = DB::table('serve_hot')->where('sid',$id)->delete();
        //判断删除是否成功
        if($status){

            return back();

        }else{

            return '删除失败';
        }
    }
}
